//
//  SYCellStatusViewModel.swift
//  SYWeibo
//
//  Created by Saya on 16/4/11.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit


class SYCellStatusViewModel: NSObject {
    var status:SYStatus?
    
    //创建属性供外部使用
    var mbrankImg:UIImage?
    var verifiedImg:UIImage?
    var userIconURL:NSURL?
    
    var repostCount:String?
    var commentCount:String?
    var zanCount:String?
    
    var source:String?
    
    init(status:SYStatus) {
        super.init()
        self.status = status
        
        setUpMbrank()
        setUpVerifiy()
        setUpSource()
        userIconURL = NSURL(string: status.user?.profile_image_url ?? "")
    
        repostCount = setWeiboCount(status.reposts_count, defaultString: "转发")
        commentCount = setWeiboCount(status.comments_count, defaultString: "评论")
        zanCount = setWeiboCount(status.attitudes_count, defaultString: "赞")
    }
    
}

extension SYCellStatusViewModel {
    private func setUpMbrank (){
        //common_icon_membership_level4
        let tempMbrank = status?.user?.mbrank ?? 0
        if tempMbrank > 0 && tempMbrank < 7 {
            self.mbrankImg = UIImage(named: "common_icon_membership_level\(tempMbrank)")
        }
    }
    
    private func setUpVerifiy (){
        let tempVerifyType = status?.user?.verified_level ?? -1
        switch tempVerifyType{
        case 1:
            self.verifiedImg = UIImage(named: "avatar_vip")
        case 2,3,5:
            self.verifiedImg = UIImage(named: "avatar_enterprise_vip")
        case 220:
            self.verifiedImg = UIImage(named: "avatar_grassroot")
        default:
            self.verifiedImg = nil
        }
    }
    
    private func setWeiboCount (count:Int,defaultString:String)->String{
        if count > 0 && count < 1000{
            return "\(count)"
        }
        
        if count >= 1000 && count < 10000{
            return "\(count/1000)千"
        }
        
        if count >= 10000 {
            return"\(Double(count/1000)/10)万"
        }
        
        return defaultString
    }
    
    //<a href=\"http://app.weibo.com/t/feed/5yvsgq\" rel=\"nofollow\">TCL乐玩2C</a>
    
    private func setUpSource(){
        let tempSpurce =  status?.source ?? ""
        
        let startFlag = "\">"
        let endFlay = "</"
        
        if let startRange = tempSpurce.rangeOfString(startFlag) , endRange = tempSpurce.rangeOfString(endFlay) {
            let range = startRange.endIndex ..< endRange.startIndex
            source = tempSpurce.substringWithRange(range)
//            print(source)
        }
    }
}
